<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <title>layui</title>
    <meta name="renderer" content="webkit">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
    <link rel="stylesheet" href="../../lib/layui-v2.5.5/css/layui.css" media="all">
    <link rel="stylesheet" href="../../lib/font-awesome-4.7.0/css/font-awesome.min.css" media="all">
    <link rel="stylesheet" href="../../css/public.css" media="all">
    <style>
        body {
            background-color: #ffffff;
        }
    </style>
</head>
<body>
<div class="layui-form layuimini-form">
    <div class="layui-form-item">
        <label class="layui-form-label">类型</label>
        <div class="layui-input-block" >
          <input type="radio" name="mType" lay-filter="mType" value='0' title="模块" >
          <input type="radio" name="mType" lay-filter="mType" value='1' title="目录" >
          <input type="radio" name="mType" lay-filter="mType" value='2' title="菜单" checked="" >
          <input type="radio" name="mType" lay-filter="mType" value='3' title="按钮">
        </div>
    </div>
    <div class="layui-form-item">
        <label class="layui-form-label required">名称</label>
        <div class="layui-input-block">
            <input type="text" name="title" id="title" lay-verify="required" lay-reqtext="模块,目录,菜单,按钮名称不能为空" placeholder="模块,目录,菜单,按钮名称" value="" class="layui-input">
        </div>
    </div>
    <div class="layui-form-item" id="href1">
        <label class="layui-form-label required">菜单url</label>
        <div class="layui-input-block">
            <input type="text" name="href" id="href" lay-verify="required" lay-reqtext="菜单url不能为空" placeholder="请输入菜单url" value="" class="layui-input">
        </div>
    </div>
    <div class="layui-form-item" id="parentid1">
        <label class="layui-form-label">父组别</label>
        <div class="layui-input-block">
            <input type="text" id="parentid" name="parentid" lay-filter="parentid" class="layui-input">
        </div>
    </div>
    <!-- <div class="layui-form-item" id="iconPicker11">
        <label for="" class="layui-form-label">图标</label>
        <div class="layui-input-block">
            <input type="text" id="iconPicker1" value="fa-arrows" lay-filter="iconPicker2" class="hide">
        </div>
    </div> -->

    <div class="layui-form-item" id="authcode1">
        <label class="layui-form-label">授权标识</label>
        <div class="layui-input-block">
            <input type="text" name="authcode" id="authcode" placeholder="授权标识" value="" class="layui-input">
        </div>
    </div>
    <div class="layui-form-item" pane="">
        <label class="layui-form-label">状态</label>
        <div class="layui-input-block">
            <input type="radio" name="status" value="1" title="有效" checked="">
            <input type="radio" name="status" value="0" title="无效">
        </div>
    </div>

    <div class="layui-form-item">
        <div class="layui-input-block">
            <button class="layui-btn layui-btn-radius layui-btn-primary" lay-submit lay-filter="saveBtn" id="saveBtn">确认保存</button>
            <button class="layui-btn layui-btn-radius layui-btn-primary"  id="backBtn">返回</button>
        </div>
    </div>
</div>
<script src="../../lib/layui-v2.5.5/layui.js" charset="utf-8"></script>
<script src="/js/lay-config.js" charset="utf-8"></script>
<script src="../../js/custorm/init.js"></script>
<script>
    layui.use(['form','treeSelect','iconPickerFa'], function () {
        var form = layui.form,
         iconPickerFa = layui.iconPickerFa,
            layer = layui.layer,treeSelect=layui.treeSelect,
            $ = layui.$;
            //var parentguid = "";
            HavePermission('menu-update')==true?$("#saveBtn").show():$("#saveBtn").hide();
            var menu={};
            $(document).ready(function(){
                var content = window.location.href;
                var id = content.split('=')[content.split('=').length - 1];
                AxajGet('../../api/sys/GetMenuById?id='+id,'../../login.html',initMenuData)
                treeSelectInit();
            });
        
        function initMenuData(data){
             menu=data;
            // parentguid=menu.parentid;
             if(menu.mType==0){
                $("#href1").hide();
                $("#href").attr("lay-verify","");
                $("#parentid1").hide();
                // $("#iconPicker11").hide();
                // $("#iconPicker1").attr("lay-verify","");
             }else if(menu.mType==1){
                $("#href1").hide();
                $("#href").attr("lay-verify","");
                $("#parentid1").show();
                // $("#iconPicker11").show();
                // $("#iconPicker1").attr("lay-verify","required");
             }else if(menu.mType==2){
                $("#href1").show();
                $("#href").attr("lay-verify","required");
                $("#parentid1").show();
                // $("#iconPicker11").show();
                // $("#iconPicker1").attr("lay-verify","required");
             }else{
                $("#href1").hide();
                $("#href").attr("lay-verify","");
                $("#parentid1").show();
                // $("#iconPicker11").hide();
                // $("#iconPicker1").attr("lay-verify","");
             }
             $("input[name='mType']").attr("disabled","disabled");
           //  $('#mType').val(menu.mType);
             $('#title').val(menu.title); 
             $('#href').val(menu.href); 
             $('#authcode').val(menu.authcode);
             $("input[name=mType][value='0']").attr("checked", data.mType == 0 ? true : false);
             $("input[name=mType][value='1']").attr("checked", data.mType == 1 ? true : false);
             $("input[name=mType][value='2']").attr("checked", data.mType == 2 ? true : false);
             $("input[name=mType][value='3']").attr("checked", data.mType == 3 ? true : false);

             $("input[name=status][value='1']").attr("checked", data.status == true ? true : false);
             $("input[name=status][value='0']").attr("checked", data.status == false ? true : false);
             form.render();

        };
        
        //监听提交
        form.on('submit(saveBtn)', function (data) {
            if((data.field.type==1||data.field.type==2||data.field.type==3)&&menu.parentid==0){
                layer.msg("父类型不能为空！");
                return;
            }
            menu.authcode=data.field.authcode;
            menu.href=data.field.href;
            menu.status=data.field.status=="1"?true:false;
            menu.title=data.field.title;
            AxajPost('../../api/sys/UpdateMenu',menu,'保存成功','../../login.html');
        });
       

        $("#backBtn").click(function () {
            window.location.href="../menu.html";
        });
        function treeSelectInit(){
            treeSelect.render({
                // 选择器
                elem: '#parentid',
                // 数据
                data: '../../api/sys/GetAllMenuTree',
                // 异步加载方式：get/post，默认get
                type: 'get',
                // 占位符
                placeholder: '请选择父组别',
                // 是否开启搜索功能：true/false，默认false
                search: true,
                async: {
                    enable: false
                },
                // 点击回调
                click: function (d) {
                   //parentguid=
                    menu.parentid = parseInt(d.current.id);
                    menu.parentname = d.current.name;
                    console.log(d);
                },
                // 加载完成后的回调函数
                    success: function (d) {
                    treeSelect.checkNode('parentid', menu.parentid);
                    treeSelect.refresh();
                }
            });
        }
        
    });
</script>
</body>
</html>